app.controller('indexcontroller', ['$scope', '$parse', '$http', '$rootScope', 'toLowerService', 'toPascalCaseService', 'eventsServices', function($scope, $parse, $http, $rootScope, toLowerService, toPascalCaseService, eventsServices)
{
	$scope.isComponentLoading = true;
	
	$scope.$on('currentUser', function(event, args) {
		$scope.currentUser = args.user;
	});
	
	/**
	* Send a contact mail
	*/
	$scope.sendContactCommand = function()
	{
		$scope.canSendContact = false;
		
		alert(toPascalCaseService.convert($scope.contactFirstName));
		
		$http.post($scope.servicebase, { "service" : "sendContact", "firstname" : toPascalCaseService.convert($scope.contactFirstName), "lastname" : toPascalCaseService.convert($scope.contactLastName), "email" : toLowerService.convert($scope.contactEmail), "object" : $scope.ContactObject, "message" : $scope.ContactMessage}).
		success(function(data, status) 
		{
			console.info("sendContactCommand - call success"); 
			$scope.status = status;
			$scope.data = data;
			$scope.result = data; 

			if(data.isFailed == true)
			{
				$scope.setMessageCommand($scope.data.exception, 'danger');
			}
			else
			{
				$scope.setMessageCommand("Votre message a été transmis", 'info');
				$("#contacts").modal('hide');
			}
			
			$scope.canSendContact = true;
		})
		.
		error(function(data, status) 
		{
			console.error("sendContactCommand - call failed"); 	
			$scope.data = data || "Request failed";
			$scope.status = status;			
			console.error("sendContactCommand - data : " + $scope.data); 				
		});
	};
	
	/**
	* Create a demand to be member
	*/
	$scope.addNewMemberCommand = function()
	{
		$scope.canAddNewMember = false;
	
		$http.post($scope.servicebase, { "service" : "demandNewMember", "firstname" : toPascalCaseService.convert($scope.newMemberFirstName), "lastname" : toPascalCaseService.convert($scope.newMemberLastName), "email" :  toLowerService.convert($scope.newMemberMail), "object" : $scope.newMemberObject, "message" : $scope.newMemberComment}).
		success(function(data, status) 
		{
			console.info("addNewMemberCommand - call success"); 
			$scope.status = status;
			$scope.data = data;
			$scope.result = data; 
			$scope.canAddNewMember = false;
			
			if(data.isFailed == true)
			{
				$scope.setMessageCommand($scope.data.exception, 'danger');
			}
			else
			{
				$scope.setMessageCommand("Votre demande a été transmise", 'info');
				$("#newMemberSubscription").modal('hide');
			}
			
			$scope.canAddNewMember = true;
		})
		.
		error(function(data, status) 
		{
			console.error("addNewMemberCommand - call failed"); 	
			$scope.canAddNewMember = false;
			$scope.data = data || "Request failed";
			$scope.status = status;			
			console.error("addNewMemberCommand - data : " + $scope.data); 				
		});
	};
	
	/**
	*	Get all articles
	*/
	$scope.getOnlineArticlesCommand = function()
	{
		console.info("getOnlineArticlesCommand - called"); 
		
		$scope.loadingArticles = true;
		
		console.info("getOnlineArticlesCommand - call getOnlinesArticlesClient"); 
		
		$http.post($scope.servicebase, { "service" : "getOnlinesArticles"}).
		success(function(data, status) 
		{
			console.info("getOnlineArticlesCommand - call success"); 
			$scope.status = status;
			$scope.data = data;
			$scope.result = data; 
			$scope.loadingArticles = false;
			
			if(data.isFailed == true)
			{
				$scope.setMessageCommand($scope.data.exception, 'danger');
			}
			
			$scope.articles = $scope.data.response;
		})
		.
		error(function(data, status) 
		{
			console.error("getOnlineArticlesCommand - call failed : " + JSON.stringify(data)); 	
			$scope.loadingArticles = false;
			$scope.data = data || "Request failed";
			$scope.status = status;			
			console.error("getOnlineArticlesCommand - data : " + $scope.data); 				
		});
	};
	
	/**
	* Get online events
	*/
	$scope.getEventsOnlineCommand = function()
	{
		console.info("getOnlineArticlesCommand - called"); 
		
		$scope.loadingEvents = true;

		//var eventsResponse = eventsServices.getOnlineEvents();

		$http.post($scope.servicebase, { "service" : "getOnlineEvents"}).
		success(function(data, status) 
		{
			console.info("getEventsOnlineCommand - call success"); 
			$scope.status = status;
			$scope.data = data;
			$scope.result = data; 
			$scope.loadingEvents = false;

			if(data.isFailed == true)
			{
				$scope.setMessageCommand($scope.data.exception, 'danger');
			}

			if($scope.data.response != undefined)
			{
				//Set UI property
				$scope.data.response.forEach(function(event) 
				{
					var presentationEvent = new Event(event);
					$scope.onlineEvents.push(presentationEvent);
				});
			}
		})
		.
		error(function(data, status) 
		{
			console.error("getEventsOnlineCommand - call failed : " + JSON.stringify(data)); 	
			$scope.loadingEvents = false;
			$scope.data = data || "Request failed";
			$scope.status = status;			
			console.error("getEventsOnlineCommand - data : " + $scope.data); 				
		});
	};
	
	/**
	* Get done events
	*/
	$scope.getDoneEventsCommand = function()
	{
		console.info("getDoneEventsCommand - called"); 
		$scope.loadingEvents = true;

		$http.post($scope.servicebase, { "service" : "getDoneEvents"}).
		success(function(data, status) 
		{
			console.info("getDoneEventsCommand - call success"); 
			$scope.status = status;
			$scope.data = data;
			$scope.result = data; 
			$scope.loadingEvents = false;
			
			if(data.isFailed == true)
			{
				$scope.setMessageCommand($scope.data.exception, 'danger');
			}
			
			if($scope.data.response != undefined)
			{
				//Set UI property
				$scope.data.response.forEach(function(event) 
				{
					var presentationEvent = new Event(event);
					$scope.doneEvents.push(presentationEvent);
				});
			}
		})
		.
		error(function(data, status) 
		{
			console.error("getDoneEventsCommand - call failed"); 	
			$scope.loadingEvents = false;
			$scope.data = data || "Request failed";
			$scope.status = status;			
			console.error("getDoneEventsCommand - data : " + $scope.data); 				
		});
	};
	
	/**
	* Load all archives pictures from the provided event
	*/
	$scope.getArchivesEventCommand = function(event)
	{
		console.info("getPicturesCommand - called"); 
			
		//Switch off all selection
		$scope.doneEvents.forEach(function(event) 
		{
			var pathId = '#item-' + event.DALObject.Id;
			$(pathId).css('background-color', 'indigo');
			$(pathId).css('color', 'white');
		});
		
		//Switch on the current selection
		var pathId = '#item-' + event.DALObject.Id;
		$(pathId).css('background-color', 'whitesmoke');
		$(pathId).css('color', 'black');

		$scope.loadingEventArchives = true;
		$scope.archivePaths = [];
		
		$http.post($scope.servicebase, { "service" : "getArchivesEvent" , "id" : event.DALObject.Id}).
		success(function(data, status) 
		{
			console.info("getPicturesCommand - call success"); 
			$scope.status = status;
			$scope.result = data; 
			$scope.loadingEventArchives = false;

			if(data.isFailed == true)
			{
				$scope.setMessageCommand(data.exception, 'danger');
			}
			
			$scope.archivePaths = data.response;
		})
		.
		error(function(data, status) 
		{
			console.error("getPicturesCommand - call failed"); 	
			$scope.loadingEventArchives = false;
			$scope.data = data || "Request failed";
			$scope.status = status;			
			console.error("getPicturesCommand - data : " + $scope.data); 				
		});
	}
	
	/**
	* Open event details
	*/
	$scope.openDetailsCommand = function(event)
	{
		$scope.currentEvent = event;
		$("#detailsView").modal('show');
	};
	
	/**
	* New user on the plateforme
	*/
	$scope.addNewUserCommand = function()
	{
		console.info("addNewUserCommand - called"); 
		try
		{
			var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
			if(re.test($scope.newUserMail) == false)
			{
				throw "L'adresse mail n'est pas valide.";
			}
			
			if($scope.newUserFirstName == "")
			{
				throw "Le prénom de l'utilisateur est obligatoire.";
			}
			
			if($scope.newUserLastName == "")
			{
				throw "Le nom de l'utilisateur est obligatoire.";
			}

			$http.post($scope.servicebase, { "service" : "addNewUser", "firstname" : toPascalCaseService.convert($scope.newUserFirstName), "lastname" : toPascalCaseService.convert($scope.newUserLastName), "email" : toLowerService.convert($scope.newUserMail)}).
		
			success(function(data, status) 
			{	
				console.info("addNewUserCommand - call success"); 
				$scope.status = status;
				$scope.data = data;
				$scope.result = data;
				if($scope.data.isFailed == true)
				{
					$scope.setMessageCommand($scope.data.exception, 'danger', 'messageAddUser');
				}
				else
				{	
					$('#subscribeAsUser').modal('hide');
					$scope.setMessageCommand('Veuillez valider votre compte via le mail envoyé suite à votre inscription.', 'info');
				}
			})
			.
			error(function(data, status) 
			{
				console.error("addNewUserCommand - call failed"); 	
				$scope.data = data || "Request failed";
				$scope.status = status;
				console.error("addNewUserCommand - data : " + $scope.data);		
			});
		}
		catch(err)
		{
			$scope.messageAddUserVisible = true;
			$scope.setMessageCommand(err, 'danger', 'messageAddUser');
		}
	}
	
	/**
	* User want to subscribe to an event
	*/
	$scope.subscribeCommand = function(event)
	{
		if($scope.currentUser == null || $scope.currentUser == undefined)
		{
			$scope.setMessageCommand('Vous devez être authentifié avant de vous inscrire.', 'warning');
			$("#authenticate").modal('show');
		}
		else
		{
			$http.post($scope.servicebase, { "service" : "preSubscribe", "userId" : $scope.currentUser.Id, "eventId" : event.DALObject.Id}).
			success(function(data, status) 
			{	
				console.info("subscribeCommand - call success"); 
				$scope.status = status;
				$scope.data = data;
				$scope.result = data;
				if($scope.data.isFailed == true)
				{
					$scope.setMessageCommand($scope.data.exception, 'danger');
				}
				else
				{
					if(data.response.Event.IsSubscriptionValidationRequired == 1)
					{
						$scope.setMessageCommand('Votre demande doit être analysée avant d\'être validée.', 'info');
					}
					else
					{
						$scope.setMessageCommand('Votre demande est validée.', 'success');
					}
				}
			})
			.
			error(function(data, status) 
			{
				console.error("subscribeCommand - call failed"); 	
				$scope.data = data || "Request failed";
				$scope.status = status;
				console.error("subscribeCommand - data : " + $scope.data);		
			});
		}
	};
	
	/**
	* Load the home component
	*/
	$scope.loadHomeComponentCommand = function() 
	{
		console.info("loadHomeComponentCommand - called");
		//Set variables
		console.info("Set default variables");
		$scope.articles = [];
		$scope.onlineEvents = [];
		$scope.doneEvents = [];
		$scope.archivePaths = [];
		$scope.canSendMessage = true;
		$scope.canAddNewMember = true;
		$scope.canSendContact = true;
		$scope.canAddNewUser = true;
		$scope.loadingEvents = true;
		
		$scope.getEventsOnlineCommand();
		$scope.getDoneEventsCommand();
		$scope.isComponentLoading = false;
	};
	
	$scope.loadHomeComponentCommand();
}]);